package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.PunchStatus;
import org.jsola.hr.dao.IAttendPunchDailyReportDAO;
import org.jsola.hr.dao.IAttendPunchRecordDAO;
import org.jsola.hr.entity.AttendPunchDailyReportDO;
import org.jsola.hr.entity.AttendPunchRecordDO;
import org.jsola.hr.query.AttendPunchRecordQuery;
import org.jsola.hr.query.AttendRecordStatQuery;
import org.jsola.hr.service.IAttendPunchRecordService;
import org.jsola.hr.service.IAttendRecordStatService;
import org.jsola.hr.vo.AttendPunchRecordListVO;
import org.jsola.hr.vo.AttendRecordStatVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 上下班考勤报表
 *
 * @author wxz
 */
@Service("hrAttendRecordStatServiceImpl")
public class AttendRecordStatServiceImpl implements IAttendRecordStatService {

    @Autowired
    private IAttendPunchDailyReportDAO attendPunchDailyReportDAO;
    @Autowired
    private IAttendPunchRecordService attendPunchRecordService;

    @Override
    public Page<AttendRecordStatVO> selectPage(AttendRecordStatQuery attendRecordStatQuery, String siteId) {

        attendRecordStatQuery.addOrder("gmtCreate", true);

        Example example = buildExample(attendRecordStatQuery, siteId);
        Page<AttendPunchDailyReportDO> page = attendPunchDailyReportDAO.selectPageByExample(example,
                attendRecordStatQuery.getPageNo(),
                attendRecordStatQuery.getPageSize());

        if (page.getEntities() == null) {
            return page.to(AttendRecordStatVO.class);
        }

        List<AttendPunchDailyReportDO> dataList = page.getEntities();
        List<AttendRecordStatVO> statDataList = new ArrayList<>();
        if (dataList != null && dataList.size() > 0) {

            dataList.forEach(data -> {

            });

        }

        Page<AttendRecordStatVO> changePage = new Page<AttendRecordStatVO>();

        changePage.setPageSize(page.getPageSize());
        changePage.setPageNo(page.getPageNo());
        changePage.setPageCount(page.getPageCount());
        changePage.setLastEntityIndex(page.getLastEntityIndex());
        changePage.setFirstEntityIndex(page.getFirstEntityIndex());
        changePage.setEntityCount(page.getEntityCount());
        changePage.setEntities(statDataList);

        return changePage;
    }

    /**
     * 根据查询参数，构建example

     * @param attendRecordStatQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(AttendRecordStatQuery attendRecordStatQuery, String siteId) {
        Example example = new Example(AttendPunchRecordDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);

        if (attendRecordStatQuery.getEmpInfoName() != null) {
            example.and().andLike("empInfoName", "%" + attendRecordStatQuery.getEmpInfoName() + "%");
        }
        if (attendRecordStatQuery.getDeptId() != null) {
            example.and().andLike("deptId", "%" + attendRecordStatQuery.getDeptId() + "%");
        }
        if (attendRecordStatQuery.getYmd() != null) {
            example.and().andEqualTo("yearly", attendRecordStatQuery.getYmd().split("-")[0]);
            example.and().andEqualTo("monthly", attendRecordStatQuery.getYmd().split("-")[1]);
            example.and().andEqualTo("daily", attendRecordStatQuery.getYmd().split("-")[2]);
        }
        if (attendRecordStatQuery.getPunchStatus() != null) {
            example.and().andEqualTo("punchStatus", attendRecordStatQuery.getPunchStatus());
        }

        // 排序
        ExampleKit.setExampleOrder(example, attendRecordStatQuery.getOrders());
        return example;
    }
}
